package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.bo.promotionbo.MarketTopicBo;
import com.cskaoyan.bean.po.ohter.MarketGoods;
import com.cskaoyan.bean.bo.promotionbo.AdminBo;
import com.cskaoyan.bean.po.promotionpo.MarketTopic;
import com.cskaoyan.bean.po.promotionpo.MarketTopicExample;
import com.cskaoyan.bean.vo.promotionvo.MarketTopicReadVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.promotion.MarketTopicMapper;

import com.cskaoyan.util.common.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-16 23:41
 */
@Service
public class AdminTopicServiceImpl implements AdminTopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<MarketTopic> getTopics(AdminBo adminBo) {
        PageHelper.startPage(adminBo.getPage(), adminBo.getLimit());
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (adminBo.getTitle() != null) {
            criteria.andTitleLike("%" + adminBo.getTitle() + "%");
        }
        if (adminBo.getSubtitle() != null) {
            criteria.andSubtitleLike("%" + adminBo.getSubtitle() + "%");
        }
        example.setOrderByClause(adminBo.getSort() + " " + adminBo.getOrder());
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExample(example);
        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopicList);
        return CommonData.data(marketTopicPageInfo);
    }

    @Transactional
    @Override
    public MarketTopic insertTopics(MarketTopicBo marketTopicBo) {
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setGoods(marketTopicBo.getGoods());
        marketTopic.setContent(marketTopicBo.getContent());
        marketTopic.setTitle(marketTopicBo.getTitle());
        marketTopic.setReadCount(marketTopicBo.getReadCount());
        marketTopic.setPicUrl(marketTopicBo.getPicUrl());
        marketTopic.setSubtitle(marketTopicBo.getSubtitle());
        marketTopic.setPrice(marketTopicBo.getPrice());
        marketTopic.setAddTime(DateUtils.time());
        marketTopic.setUpdateTime(DateUtils.time());
        marketTopic.setDeleted(false);
        marketTopicMapper.insertSelective(marketTopic);
        return marketTopic;
    }

    @Transactional
    @Override
    public MarketTopic updateTopics(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(DateUtils.time());
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
        return marketTopic;

    }

    @Transactional
    @Override
    public void deleteTopics(MarketTopic marketTopic) {
        marketTopic.setDeleted(true);
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic);

    }
    @Transactional
    @Override
    public void deleteBatchTopics(Integer id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        marketTopic.setDeleted(true);
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
    }

    @Override
    public MarketTopicReadVo selectTopicAndGoodsById(Integer id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        Integer[] integers = marketTopic.getGoods();
        List<MarketGoods> goodsList = new ArrayList<>();
        for (Integer ids : integers) {
            goodsList.add(marketGoodsMapper.selectByPrimaryKey(ids));
        }
        return new MarketTopicReadVo(goodsList, marketTopic);
    }
}
